package com.ymatou.iapi.godmch.testcase;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.editmchBean;
import com.ymatou.iapi.godmch.parameter.getInviteDistributorPicBean;
import com.ymatou.iapi.godmch.parameter.registerByCodeBean;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.getInviteDistributorOrganizationPicCall;
import com.ymatou.iapi.godmch.service.getInviteDistributorPicCall;
import com.ymatou.iapi.godmch.service.registerByCodeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.business.service.shenqi.SqDistributorService;
import com.ymttest.database.model.invite_code;
import com.ymttest.database.sqlwapper.godmch.invite_codeWapper;
import com.ymttest.utils.TestDataManager;
/**
*获取邀请分销员分享图片
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getInviteDistributorPic{
	private static getInviteDistributorPicBean bean;
	private static getInviteDistributorPicCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取邀请分销员分享图片");
	}

	@Before
	public void caseUp() {
		bean=new getInviteDistributorPicBean();
		call=new getInviteDistributorPicCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	 private void check_verify(Integer shopId) throws InterruptedException {
		 Logger.verifyEquals("200", call.getStatus1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			String resp_code=call.getResult().get("inviteCode").getAsString();
			invite_codeWapper codeWapper=new invite_codeWapper();
			Map<String,Object> map=new HashedMap();
			map.put("inviteCode", resp_code);
			List<invite_code> codes=codeWapper.selectby(map);
			Logger.verifyEquals(1, codes.size(), "验证invite_code表新增一条数据");
			if(codes.size()>0)
			{
				invite_code code=codes.get(0);
				Logger.verifyEquals(true,call.getResult().get("picUrl").toString().length()>0, "验证picUrl不为空");
				Logger.verifyEquals(4,code.getCodeRole(), "验证invite_code表中的CodeRole=4");
				Logger.verifyEquals(bean.getDistributorId(),code.getMchId(), "验证invite_code表中的MchId有值");
				Logger.verifyEquals(shopId,code.getShopId(), "验证invite_code表中的ShopId有值");
			}

	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getInviteDistributorPic_001() {
		Logger.start(true,"获取邀请分销员分享图片-正常查询-distributorId是商户-返回成功");
		try {
			int userId=GodmchManageCallService.CreateSellerMch();
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(userId);
			GodmchManageCallService.editmachCall(editmchBean);
			bean.setDistributorId(userId);
			call.setData(bean);
			call.callService();			
			
			check_verify(userId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getInviteDistributorPic_002() {
		Logger.start(true,"获取邀请分销员分享图片-distributorId是分销员");
		try {

			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            Integer mchId=createIndepMchCall.getmchId();
            Logger.comment("###########独立商户："+mchId);
            SqDistributorService.createAutoAuditDistributor((long)mchId);
            
        	//独立商户邀请分享图
            Logger.comment("###########独立商户图片：");
            getInviteDistributorPicBean getInviteDistributorPicBean=GodmchManageCallService.initGetInviteDistributorPicBean(mchId);
            getInviteDistributorPicCall getInviteDistributorPicCall=GodmchManageCallService.getInviteDistributorPicBeanCall(getInviteDistributorPicBean);
        
            Logger.comment("###########独立商户审核：");
            Thread.sleep(5000);

            registerByCodeBean registerByCodeBean= GodmchManageCallService.initRegisterByCode(mchId, getInviteDistributorPicCall.getResult().get("inviteCode").getAsString());
            GodmchManageCallService.registerByCodeCall(registerByCodeBean);
            
            bean.setDistributorId(mchId);
			call.setData(bean);
			call.callService();			
			
			check_verify(mchId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getInviteDistributorPic_003() {
		Logger.start(true,"获取邀请分销员分享图片-distributorId用户表不存在-返回失败");
		try {
			
		 bean.setDistributorId(9999);
		 call.setData(bean);
		 call.callService();		
		 
		 Logger.verifyEquals("5000", call.getStatus1(), "验证status");
	     Logger.verifyEquals(true, call.getMessage().contains("未获取到邀请者信息"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getInviteDistributorPic_004() {
		Logger.start(true,"获取邀请分销员分享图片-distributorId不是分销员角色-平台商户");
		try {
			
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			bean.setDistributorId(mchId);
			call.setData(bean);
			call.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getInviteDistributorPic_005() {
		Logger.start(true,"获取邀请分销员分享图片-distributorId是分销员(分销机构下的分销员 禁止再创建分销员邀请码 20200807)");
		try {

			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            Integer mchId=createIndepMchCall.getmchId();
            Logger.comment("###########独立商户："+mchId);
            SqDistributorService.createAutoAuditDistributor((long)mchId);
            
        	//独立商户邀请分享图
            Logger.comment("########### 获取 分销机构邀请码：");
            getInviteDistributorOrganizationPicCall getInviteDistributorOrganizationPicCall=GodmchManageCallService.getInviteDistributorOrganizationPic(mchId);
            Thread.sleep(5000);
            //注册分销机构
            registerByCodeBean registerByCodeBean= GodmchManageCallService.initRegisterByCode(mchId, getInviteDistributorOrganizationPicCall.getInviteCode());
            registerByCodeCall registerByCodeCall=GodmchManageCallService.registerByCodeCall(registerByCodeBean);
            
            //分销机构下 邀请分销员分享图片
            getInviteDistributorPicBean  getInviteDistributorPicBean=GodmchManageCallService.initGetInviteDistributorPicBean(registerByCodeCall.getRegMchId());
            getInviteDistributorPicCall  getInviteDistributorPicCall=GodmchManageCallService.getInviteDistributorPicBeanCall(getInviteDistributorPicBean);
            //分享员 注册下级分享员
            registerByCodeBean= GodmchManageCallService.initRegisterByCode(mchId, getInviteDistributorPicCall.getResult().get("inviteCode").getAsString());
            registerByCodeCall=GodmchManageCallService.registerByCodeCall(registerByCodeBean);
             
            bean.setDistributorId(registerByCodeCall.getRegMchId());
		  	call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatus1(), "验证status");
		    Logger.verifyEquals(true, call.getMessage().contains("分销机构的分销员禁止再邀请下级分销员"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}